package com.xinxin.facelinker.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chatuidemo.R;
import com.easemob.util.ImageUtils;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.xinxin.facelinker.adapter.ImageBrowserAdapter;
import com.xinxin.facelinker.ui.button.views.Button;

import java.io.IOException;

public class ImageBrowserActivity extends Activity {

    private ViewPager vpImageBrowser;
    private TextView tvPageNum;
    private TextView tvSave;
    private int position;
    private String[] motionPhotoUrl;
    private ImageBrowserAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_browser);

        initData();
        initView();
        setData();
    }

    private void setData() {
        adapter=new ImageBrowserAdapter(this,motionPhotoUrl);
        vpImageBrowser.setAdapter(adapter);

        final int size = motionPhotoUrl.length;
        int initPosition = Integer.MAX_VALUE / 2 / size * size + position;

        if(size > 1) {
            tvPageNum.setVisibility(View.VISIBLE);
            tvPageNum.setText((position+1) + "/" + size);
        } else {
            tvPageNum.setVisibility(View.GONE);
        }

        vpImageBrowser.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                int index=position%size;
                tvPageNum.setText((index+1) + "/" + size);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        vpImageBrowser.setCurrentItem(initPosition);
    }

    private void initView() {

        vpImageBrowser= (ViewPager) findViewById(R.id.vpImageBrowser);
        tvPageNum= (TextView) findViewById(R.id.tvPageNum);
        tvSave= (TextView) findViewById(R.id.tvSave);

        tvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bitmap = adapter.getBitmap(vpImageBrowser.getCurrentItem());

                boolean showOriImag = true;
                String fileName = "img-" + (showOriImag?"ori-" : "mid-") + motionPhotoUrl[position];

                String title = fileName.substring(0, fileName.lastIndexOf("."));
                String insertImage = MediaStore.Images.Media.insertImage(
                        getContentResolver(), bitmap, title, "FLImage");
                if(insertImage == null) {
                    Toast.makeText(ImageBrowserActivity.this,"无法保存",Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(ImageBrowserActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
                }

			try {
				com.xinxin.facelinker.utils.ImageUtils.saveFile(ImageBrowserActivity.this, bitmap, fileName);
			} catch (IOException e) {
				e.printStackTrace();
			}

            }
        });

    }

    private void initData() {
        motionPhotoUrl=getIntent().getStringArrayExtra("motionPhotoUrl");
        position=getIntent().getIntExtra("position",0);
    }
}
